<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>

<title>Ruby on Rails 2.2 Release Notes — Ruby on Rails Guides</title>
<link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />

<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" />
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" />

<link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" />

<link href="images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
</head>
<body class="guide">
  <div id="topNav">
    <div class="wrapper">
      <strong class="more-info-label">更多内容 <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
      <span class="red-button more-info-button">
        更多内容
      </span>
      <ul class="more-info-links s-hidden">
        <li class="more-info"><a href="http://weblog.rubyonrails.org/">博客</a></li>
        <li class="more-info"><a href="http://guides.rubyonrails.org/">指南</a></li>
        <li class="more-info"><a href="http://api.rubyonrails.org/">API</a></li>
        <li class="more-info"><a href="http://stackoverflow.com/questions/tagged/ruby-on-rails">提问</a></li>
        <li class="more-info"><a href="https://github.com/rails/rails">到 GitHub 贡献</a></li>
        <li class="more-info"><a href="https://ruby-china.org/">Ruby China 社区</a></li>
      </ul>
    </div>
  </div>
  <div id="header">
    <div class="wrapper clearfix">
      <h1><a href="index.html" title="返回首页">Rails 指南</a></h1>
      <ul class="nav">
        <li><a class="nav-item" href="index.html">首页</a></li>
        <li class="guides-index guides-index-large">
          <a href="index.html" id="guidesMenu" class="guides-index-item nav-item">指南索引</a>
          <div id="guides" class="clearfix" style="display: none;">
            <hr />
              <dl class="L">
                <dt>新手入门</dt>
                <dd><a href="getting_started.html">Rails 入门</a></dd>
                <dt>模型</dt>
                <dd><a href="active_record_basics.html">Active Record 基础</a></dd>
                <dd><a href="active_record_migrations.html">Active Record 迁移</a></dd>
                <dd><a href="active_record_validations.html">Active Record 数据验证</a></dd>
                <dd><a href="active_record_callbacks.html">Active Record 回调</a></dd>
                <dd><a href="association_basics.html">Active Record 关联</a></dd>
                <dd><a href="active_record_querying.html">Active Record 查询接口</a></dd>
                <dt>视图</dt>
                <dd><a href="layouts_and_rendering.html">Rails 布局和视图渲染</a></dd>
                <dd><a href="form_helpers.html">Action View 表单辅助方法</a></dd>
                <dt>控制器</dt>
                <dd><a href="action_controller_overview.html">Action Controller 概览</a></dd>
                <dd><a href="routing.html">Rails 路由全解</a></dd>
              </dl>
              <dl class="R">
                <dt>深入探索</dt>
                <dd><a href="active_support_core_extensions.html">Active Support 核心扩展</a></dd>
                <dd><a href="i18n.html">Rails 国际化 API</a></dd>
                <dd><a href="action_mailer_basics.html">Action Mailer 基础</a></dd>
                <dd><a href="active_job_basics.html">Active Job 基础</a></dd>
                <dd><a href="testing.html">Rails 应用测试指南</a></dd>
                <dd><a href="security.html">Ruby on Rails 安全指南</a></dd>
                <dd><a href="debugging_rails_applications.html">调试 Rails 应用</a></dd>
                <dd><a href="configuring.html">配置 Rails 应用</a></dd>
                <dd><a href="command_line.html">Rails 命令行</a></dd>
                <dd><a href="asset_pipeline.html">Asset Pipeline</a></dd>
                <dd><a href="working_with_javascript_in_rails.html">在 Rails 中使用 JavaScript</a></dd>
                <dd><a href="autoloading_and_reloading_constants.html">自动加载和重新加载常量</a></dd>
                <dd><a href="caching_with_rails.html">Rails 缓存概览</a></dd>
                <dd><a href="api_app.html">使用 Rails 开发只提供 API 的应用</a></dd>
                <dd><a href="action_cable_overview.html">Action Cable 概览</a></dd>
                <dt>扩展 Rails</dt>
                <dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
                <dd><a href="generators.html">创建及定制 Rails 生成器和模板</a></dd>
                <dt>为 Ruby on Rails 做贡献</dt>
                <dd><a href="contributing_to_ruby_on_rails.html">为 Ruby on Rails 做贡献</a></dd>
                <dd><a href="api_documentation_guidelines.html">API 文档指导方针</a></dd>
                <dd><a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails 指南指导方针</a></dd>
                <dt>维护方针</dt>
                <dd><a href="maintenance_policy.html">Ruby on Rails 的维护方针</a></dd>
                <dt>发布记</dt>
                <dd><a href="upgrading_ruby_on_rails.html">Ruby on Rails 升级指南</a></dd>
                <dd><a href="5_0_release_notes.html">Ruby on Rails 5.0 发布记</a></dd>
                <dd><a href="4_2_release_notes.html">Ruby on Rails 4.2 发布记</a></dd>
                <dd><a href="4_1_release_notes.html">Ruby on Rails 4.1 发布记</a></dd>
                <dd><a href="4_0_release_notes.html">Ruby on Rails 4.0 发布记</a></dd>
                <dd><a href="3_2_release_notes.html">Ruby on Rails 3.2 发布记</a></dd>
                <dd><a href="3_1_release_notes.html">Ruby on Rails 3.1 发布记</a></dd>
                <dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 发布记</a></dd>
                <dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 发布记</a></dd>
                <dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 发布记</a></dd>
              </dl>
          </div>
        </li>
        <li><a class="nav-item" href="contributing_to_ruby_on_rails.html">贡献</a></li>
        <li><a class="nav-item" href="credits.html">感谢</a></li>
        <li class="guides-index guides-index-small">
          <select class="guides-index-item nav-item">
            <option value="index.html">指南索引</option>
              <optgroup label="新手入门">
                  <option value="getting_started.html">Rails 入门</option>
              </optgroup>
              <optgroup label="模型">
                  <option value="active_record_basics.html">Active Record 基础</option>
                  <option value="active_record_migrations.html">Active Record 迁移</option>
                  <option value="active_record_validations.html">Active Record 数据验证</option>
                  <option value="active_record_callbacks.html">Active Record 回调</option>
                  <option value="association_basics.html">Active Record 关联</option>
                  <option value="active_record_querying.html">Active Record 查询接口</option>
              </optgroup>
              <optgroup label="视图">
                  <option value="layouts_and_rendering.html">Rails 布局和视图渲染</option>
                  <option value="form_helpers.html">Action View 表单辅助方法</option>
              </optgroup>
              <optgroup label="控制器">
                  <option value="action_controller_overview.html">Action Controller 概览</option>
                  <option value="routing.html">Rails 路由全解</option>
              </optgroup>
              <optgroup label="深入探索">
                  <option value="active_support_core_extensions.html">Active Support 核心扩展</option>
                  <option value="i18n.html">Rails 国际化 API</option>
                  <option value="action_mailer_basics.html">Action Mailer 基础</option>
                  <option value="active_job_basics.html">Active Job 基础</option>
                  <option value="testing.html">Rails 应用测试指南</option>
                  <option value="security.html">Ruby on Rails 安全指南</option>
                  <option value="debugging_rails_applications.html">调试 Rails 应用</option>
                  <option value="configuring.html">配置 Rails 应用</option>
                  <option value="command_line.html">Rails 命令行</option>
                  <option value="asset_pipeline.html">Asset Pipeline</option>
                  <option value="working_with_javascript_in_rails.html">在 Rails 中使用 JavaScript</option>
                  <option value="autoloading_and_reloading_constants.html">自动加载和重新加载常量</option>
                  <option value="caching_with_rails.html">Rails 缓存概览</option>
                  <option value="api_app.html">使用 Rails 开发只提供 API 的应用</option>
                  <option value="action_cable_overview.html">Action Cable 概览</option>
              </optgroup>
              <optgroup label="扩展 Rails">
                  <option value="rails_on_rack.html">Rails on Rack</option>
                  <option value="generators.html">创建及定制 Rails 生成器和模板</option>
              </optgroup>
              <optgroup label="为 Ruby on Rails 做贡献">
                  <option value="contributing_to_ruby_on_rails.html">为 Ruby on Rails 做贡献</option>
                  <option value="api_documentation_guidelines.html">API 文档指导方针</option>
                  <option value="ruby_on_rails_guides_guidelines.html">Ruby on Rails 指南指导方针</option>
              </optgroup>
              <optgroup label="维护方针">
                  <option value="maintenance_policy.html">Ruby on Rails 的维护方针</option>
              </optgroup>
              <optgroup label="发布记">
                  <option value="upgrading_ruby_on_rails.html">Ruby on Rails 升级指南</option>
                  <option value="5_0_release_notes.html">Ruby on Rails 5.0 发布记</option>
                  <option value="4_2_release_notes.html">Ruby on Rails 4.2 发布记</option>
                  <option value="4_1_release_notes.html">Ruby on Rails 4.1 发布记</option>
                  <option value="4_0_release_notes.html">Ruby on Rails 4.0 发布记</option>
                  <option value="3_2_release_notes.html">Ruby on Rails 3.2 发布记</option>
                  <option value="3_1_release_notes.html">Ruby on Rails 3.1 发布记</option>
                  <option value="3_0_release_notes.html">Ruby on Rails 3.0 发布记</option>
                  <option value="2_3_release_notes.html">Ruby on Rails 2.3 发布记</option>
                  <option value="2_2_release_notes.html">Ruby on Rails 2.2 发布记</option>
              </optgroup>
          </select>
        </li>
      </ul>
    </div>
  </div>
  <hr class="hide" />

  <div id="feature">
    <div class="wrapper">
      <h2>Ruby on Rails 2.2 Release Notes</h2><p>Rails 2.2 delivers a number of new and improved features. This list covers the major upgrades, but doesn&#39;t include every little bug fix and change. If you want to see everything, check out the <a href="http://github.com/rails/rails/commits/2-2-stable">list of commits</a> in the main Rails repository on GitHub.</p><p>Along with Rails, 2.2 marks the launch of the <a href="http://guides.rubyonrails.org/">Ruby on Rails Guides</a>, the first results of the ongoing <a href="http://hackfest.rubyonrails.org/guide">Rails Guides hackfest</a>. This site will deliver high-quality documentation of the major features of Rails.</p>

              <div id="subCol">
          <h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />目录</h3>
          <ol class="chapters">
<li>
<a href="#infrastructure">Infrastructure</a>

<ul>
<li><a href="#internationalization">Internationalization</a></li>
<li><a href="#compatibility-with-ruby-1-9-and-jruby">Compatibility with Ruby 1.9 and JRuby</a></li>
</ul>
</li>
<li><a href="#documentation">Documentation</a></li>
<li><a href="#better-integration-with-http-out-of-the-box-etag-support">Better integration with HTTP : Out of the box ETag support</a></li>
<li><a href="#thread-safety">Thread Safety</a></li>
<li>
<a href="#active-record">Active Record</a>

<ul>
<li><a href="#transactional-migrations">Transactional Migrations</a></li>
<li><a href="#connection-pooling">Connection Pooling</a></li>
<li><a href="#hashes-for-join-table-conditions">Hashes for Join Table Conditions</a></li>
<li><a href="#new-dynamic-finders">New Dynamic Finders</a></li>
<li><a href="#associations-respect-private-protected-scope">Associations Respect Private/Protected Scope</a></li>
<li><a href="#other-active-record-changes">Other Active Record Changes</a></li>
</ul>
</li>
<li>
<a href="#action-controller">Action Controller</a>

<ul>
<li><a href="#shallow-route-nesting">Shallow Route Nesting</a></li>
<li><a href="#method-arrays-for-member-or-collection-routes">Method Arrays for Member or Collection Routes</a></li>
<li><a href="#resources-with-specific-actions">Resources With Specific Actions</a></li>
<li><a href="#other-action-controller-changes">Other Action Controller Changes</a></li>
</ul>
</li>
<li><a href="#action-view">Action View</a></li>
<li><a href="#action-mailer">Action Mailer</a></li>
<li>
<a href="#active-support">Active Support</a>

<ul>
<li><a href="#memoization">Memoization</a></li>
<li><a href="#each-with-object">each_with_object</a></li>
<li><a href="#delegates-with-prefixes">Delegates With Prefixes</a></li>
<li><a href="#other-active-support-changes">Other Active Support Changes</a></li>
</ul>
</li>
<li>
<a href="#railties">Railties</a>

<ul>
<li><a href="#config-gems">config.gems</a></li>
<li><a href="#other-railties-changes">Other Railties Changes</a></li>
</ul>
</li>
<li><a href="#deprecated">Deprecated</a></li>
<li><a href="#credits">Credits</a></li>
</ol>

        </div>

    </div>
  </div>

  <div id="container">
    <div class="wrapper">
      <div id="mainCol">
        <h3 id="infrastructure">1 Infrastructure</h3><p>Rails 2.2 is a significant release for the infrastructure that keeps Rails humming along and connected to the rest of the world.</p><h4 id="internationalization">1.1 Internationalization</h4><p>Rails 2.2 supplies an easy system for internationalization (or i18n, for those of you tired of typing).</p>
<ul>
<li>Lead Contributors: Rails i18 Team</li>
<li>More information :

<ul>
<li><a href="http://rails-i18n.org">Official Rails i18 website</a></li>
<li><a href="https://web.archive.org/web/20140407075019/http://www.artweb-design.de/2008/7/18/finally-ruby-on-rails-gets-internationalized">Finally. Ruby on Rails gets internationalized</a></li>
<li><a href="http://github.com/clemens/i18n_demo_app">Localizing Rails : Demo application</a></li>
</ul>
</li>
</ul>
<h4 id="compatibility-with-ruby-1-9-and-jruby">1.2 Compatibility with Ruby 1.9 and JRuby</h4><p>Along with thread safety, a lot of work has been done to make Rails work well with JRuby and the upcoming Ruby 1.9. With Ruby 1.9 being a moving target, running edge Rails on edge Ruby is still a hit-or-miss proposition, but Rails is ready to make the transition to Ruby 1.9 when the latter is released.</p><h3 id="documentation">2 Documentation</h3><p>The internal documentation of Rails, in the form of code comments, has been improved in numerous places. In addition, the <a href="http://guides.rubyonrails.org/">Ruby on Rails Guides</a> project is the definitive source for information on major Rails components. In its first official release, the Guides page includes:</p>
<ul>
<li><a href="getting_started.html">Getting Started with Rails</a></li>
<li><a href="migrations.html">Rails Database Migrations</a></li>
<li><a href="association_basics.html">Active Record Associations</a></li>
<li><a href="active_record_querying.html">Active Record Query Interface</a></li>
<li><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></li>
<li><a href="form_helpers.html">Action View Form Helpers</a></li>
<li><a href="routing.html">Rails Routing from the Outside In</a></li>
<li><a href="action_controller_overview.html">Action Controller Overview</a></li>
<li><a href="caching_with_rails.html">Rails Caching</a></li>
<li><a href="testing.html">A Guide to Testing Rails Applications</a></li>
<li><a href="security.html">Securing Rails Applications</a></li>
<li><a href="debugging_rails_applications.html">Debugging Rails Applications</a></li>
<li><a href="performance_testing.html">Performance Testing Rails Applications</a></li>
<li><a href="plugins.html">The Basics of Creating Rails Plugins</a></li>
</ul>
<p>All told, the Guides provide tens of thousands of words of guidance for beginning and intermediate Rails developers.</p><p>If you want to generate these guides locally, inside your application:</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
rake doc:guides

</pre>
</div>
<p>This will put the guides inside <code>Rails.root/doc/guides</code> and you may start surfing straight away by opening <code>Rails.root/doc/guides/index.html</code> in your favourite browser.</p>
<ul>
<li>Lead Contributors: <a href="credits.html">Rails Documentation Team</a>
</li>
<li>Major contributions from <a href="http://advogato.org/person/fxn/diary.html">Xavier Noria</a> and <a href="http://izumi.plan99.net/blog/">Hongli Lai</a>.</li>
<li>More information:

<ul>
<li><a href="http://hackfest.rubyonrails.org/guide">Rails Guides hackfest</a></li>
<li><a href="http://weblog.rubyonrails.org/2008/5/2/help-improve-rails-documentation-on-git-branch">Help improve Rails documentation on Git branch</a></li>
</ul>
</li>
</ul>
<h3 id="better-integration-with-http-out-of-the-box-etag-support">3 Better integration with HTTP : Out of the box ETag support</h3><p>Supporting the etag and last modified timestamp in HTTP headers means that Rails can now send back an empty response if it gets a request for a resource that hasn't been modified lately. This allows you to check whether a response needs to be sent at all.</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class ArticlesController &lt; ApplicationController
  def show_with_respond_to_block
    @article = Article.find(params[:id])

    # If the request sends headers that differs from the options provided to stale?, then
    # the request is indeed stale and the respond_to block is triggered (and the options
    # to the stale? call is set on the response).
    #
    # If the request headers match, then the request is fresh and the respond_to block is
    # not triggered. Instead the default render will occur, which will check the last-modified
    # and etag headers and conclude that it only needs to send a "304 Not Modified" instead
    # of rendering the template.
    if stale?(:last_modified =&gt; @article.published_at.utc, :etag =&gt; @article)
      respond_to do |wants|
        # normal response processing
      end
    end
  end

  def show_with_implied_render
    @article = Article.find(params[:id])

    # Sets the response headers and checks them against the request, if the request is stale
    # (i.e. no match of either etag or last-modified), then the default render of the template happens.
    # If the request is fresh, then the default render will return a "304 Not Modified"
    # instead of rendering the template.
    fresh_when(:last_modified =&gt; @article.published_at.utc, :etag =&gt; @article)
  end
end

</pre>
</div>
<h3 id="thread-safety">4 Thread Safety</h3><p>The work done to make Rails thread-safe is rolling out in Rails 2.2. Depending on your web server infrastructure, this means you can handle more requests with fewer copies of Rails in memory, leading to better server performance and higher utilization of multiple cores.</p><p>To enable multithreaded dispatching in production mode of your application, add the following line in your <code>config/environments/production.rb</code>:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
config.threadsafe!

</pre>
</div>

<ul>
<li>More information :

<ul>
<li><a href="http://m.onkey.org/2008/10/23/thread-safety-for-your-rails">Thread safety for your Rails</a></li>
<li><a href="http://weblog.rubyonrails.org/2008/8/16/josh-peek-officially-joins-the-rails-core">Thread safety project announcement</a></li>
<li><a href="http://blog.headius.com/2008/08/qa-what-thread-safe-rails-means.html">Q/A: What Thread-safe Rails Means</a></li>
</ul>
</li>
</ul>
<h3 id="active-record">5 Active Record</h3><p>There are two big additions to talk about here: transactional migrations and pooled database transactions. There's also a new (and cleaner) syntax for join table conditions, as well as a number of smaller improvements.</p><h4 id="transactional-migrations">5.1 Transactional Migrations</h4><p>Historically, multiple-step Rails migrations have been a source of trouble. If something went wrong during a migration, everything before the error changed the database and everything after the error wasn't applied. Also, the migration version was stored as having been executed, which means that it couldn't be simply rerun by <code>rake db:migrate:redo</code> after you fix the problem. Transactional migrations change this by wrapping migration steps in a DDL transaction, so that if any of them fail, the entire migration is undone. In Rails 2.2, transactional migrations are supported on PostgreSQL out of the box. The code is extensible to other database types in the future - and IBM has already extended it to support the DB2 adapter.</p>
<ul>
<li>Lead Contributor: <a href="http://adam.heroku.com/">Adam Wiggins</a>
</li>
<li>More information:

<ul>
<li><a href="http://adam.heroku.com/past/2008/9/3/ddl_transactions/">DDL Transactions</a></li>
<li><a href="http://db2onrails.com/2008/11/08/a-major-milestone-for-db2-on-rails/">A major milestone for DB2 on Rails</a></li>
</ul>
</li>
</ul>
<h4 id="connection-pooling">5.2 Connection Pooling</h4><p>Connection pooling lets Rails distribute database requests across a pool of database connections that will grow to a maximum size (by default 5, but you can add a <code>pool</code> key to your <code>database.yml</code> to adjust this). This helps remove bottlenecks in applications that support many concurrent users. There's also a <code>wait_timeout</code> that defaults to 5 seconds before giving up. <code>ActiveRecord::Base.connection_pool</code> gives you direct access to the pool if you need it.</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
development:
  adapter: mysql
  username: root
  database: sample_development
  pool: 10
  wait_timeout: 10

</pre>
</div>

<ul>
<li>Lead Contributor: <a href="http://blog.nicksieger.com/">Nick Sieger</a>
</li>
<li>More information:

<ul>
<li><a href="http://archives.ryandaigle.com/articles/2008/9/7/what-s-new-in-edge-rails-connection-pools">What's New in Edge Rails: Connection Pools</a></li>
</ul>
</li>
</ul>
<h4 id="hashes-for-join-table-conditions">5.3 Hashes for Join Table Conditions</h4><p>You can now specify conditions on join tables using a hash. This is a big help if you need to query across complex joins.</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class Photo &lt; ActiveRecord::Base
  belongs_to :product
end

class Product &lt; ActiveRecord::Base
  has_many :photos
end

# Get all products with copyright-free photos:
Product.all(:joins =&gt; :photos, :conditions =&gt; { :photos =&gt; { :copyright =&gt; false }})

</pre>
</div>

<ul>
<li>More information:

<ul>
<li><a href="http://archives.ryandaigle.com/articles/2008/7/7/what-s-new-in-edge-rails-easy-join-table-conditions">What's New in Edge Rails: Easy Join Table Conditions</a></li>
</ul>
</li>
</ul>
<h4 id="new-dynamic-finders">5.4 New Dynamic Finders</h4><p>Two new sets of methods have been added to Active Record's dynamic finders family.</p><h5 id="find-last-by-attribute">5.4.1 <code>find_last_by_attribute</code>
</h5><p>The <code>find_last_by_attribute</code> method is equivalent to <code>Model.last(:conditions =&gt; {:attribute =&gt; value})</code></p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
# Get the last user who signed up from London
User.find_last_by_city('London')

</pre>
</div>

<ul>
<li>Lead Contributor: <a href="http://www.workingwithrails.com/person/9147-emilio-tagua">Emilio Tagua</a>
</li>
</ul>
<h5 id="find-by-attribute-bang">5.4.2 <code>find_by_attribute!</code>
</h5><p>The new bang! version of <code>find_by_attribute!</code> is equivalent to <code>Model.first(:conditions =&gt; {:attribute =&gt; value}) || raise ActiveRecord::RecordNotFound</code> Instead of returning <code>nil</code> if it can't find a matching record, this method will raise an exception if it cannot find a match.</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
# Raise ActiveRecord::RecordNotFound exception if 'Moby' hasn't signed up yet!
User.find_by_name!('Moby')

</pre>
</div>

<ul>
<li>Lead Contributor: <a href="http://blog.hasmanythrough.com">Josh Susser</a>
</li>
</ul>
<h4 id="associations-respect-private-protected-scope">5.5 Associations Respect Private/Protected Scope</h4><p>Active Record association proxies now respect the scope of methods on the proxied object. Previously (given User has_one :account) <code>@user.account.private_method</code> would call the private method on the associated Account object. That fails in Rails 2.2; if you need this functionality, you should use <code>@user.account.send(:private_method)</code> (or make the method public instead of private or protected). Please note that if you're overriding <code>method_missing</code>, you should also override <code>respond_to</code> to match the behavior in order for associations to function normally.</p>
<ul>
<li>Lead Contributor: Adam Milligan</li>
<li>More information:

<ul>
<li><a href="http://afreshcup.com/2008/10/24/rails-22-change-private-methods-on-association-proxies-are-private/">Rails 2.2 Change: Private Methods on Association Proxies are Private</a></li>
</ul>
</li>
</ul>
<h4 id="other-active-record-changes">5.6 Other Active Record Changes</h4>
<ul>
<li>
<code>rake db:migrate:redo</code> now accepts an optional VERSION to target that specific migration to redo</li>
<li>Set <code>config.active_record.timestamped_migrations = false</code> to have migrations with numeric prefix instead of UTC timestamp.</li>
<li>Counter cache columns (for associations declared with <code>:counter_cache =&gt; true</code>) do not need to be initialized to zero any longer.</li>
<li>
<code>ActiveRecord::Base.human_name</code> for an internationalization-aware humane translation of model names</li>
</ul>
<h3 id="action-controller">6 Action Controller</h3><p>On the controller side, there are several changes that will help tidy up your routes. There are also some internal changes in the routing engine to lower memory usage on complex applications.</p><h4 id="shallow-route-nesting">6.1 Shallow Route Nesting</h4><p>Shallow route nesting provides a solution to the well-known difficulty of using deeply-nested resources. With shallow nesting, you need only supply enough information to uniquely identify the resource that you want to work with.</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
map.resources :publishers, :shallow =&gt; true do |publisher|
  publisher.resources :magazines do |magazine|
    magazine.resources :photos
  end
end

</pre>
</div>
<p>This will enable recognition of (among others) these routes:</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
/publishers/1           ==&gt; publisher_path(1)
/publishers/1/magazines ==&gt; publisher_magazines_path(1)
/magazines/2            ==&gt; magazine_path(2)
/magazines/2/photos     ==&gt; magazines_photos_path(2)
/photos/3               ==&gt; photo_path(3)

</pre>
</div>

<ul>
<li>Lead Contributor: <a href="http://www.unwwwired.net/">S. Brent Faulkner</a>
</li>
<li>More information:

<ul>
<li><a href="routing.html#nested-resources">Rails Routing from the Outside In</a></li>
<li><a href="http://archives.ryandaigle.com/articles/2008/9/7/what-s-new-in-edge-rails-shallow-routes">What's New in Edge Rails: Shallow Routes</a></li>
</ul>
</li>
</ul>
<h4 id="method-arrays-for-member-or-collection-routes">6.2 Method Arrays for Member or Collection Routes</h4><p>You can now supply an array of methods for new member or collection routes. This removes the annoyance of having to define a route as accepting any verb as soon as you need it to handle more than one. With Rails 2.2, this is a legitimate route declaration:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
map.resources :photos, :collection =&gt; { :search =&gt; [:get, :post] }

</pre>
</div>

<ul>
<li>Lead Contributor: <a href="http://brennandunn.com/">Brennan Dunn</a>
</li>
</ul>
<h4 id="resources-with-specific-actions">6.3 Resources With Specific Actions</h4><p>By default, when you use <code>map.resources</code> to create a route, Rails generates routes for seven default actions (index, show, create, new, edit, update, and destroy). But each of these routes takes up memory in your application, and causes Rails to generate additional routing logic. Now you can use the <code>:only</code> and <code>:except</code> options to fine-tune the routes that Rails will generate for resources. You can supply a single action, an array of actions, or the special <code>:all</code> or <code>:none</code> options. These options are inherited by nested resources.</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
map.resources :photos, :only =&gt; [:index, :show]
map.resources :products, :except =&gt; :destroy

</pre>
</div>

<ul>
<li>Lead Contributor: <a href="http://experthuman.com/">Tom Stuart</a>
</li>
</ul>
<h4 id="other-action-controller-changes">6.4 Other Action Controller Changes</h4>
<ul>
<li>You can now easily <a href="http://m.onkey.org/2008/7/20/rescue-from-dispatching">show a custom error page</a> for exceptions raised while routing a request.</li>
<li>The HTTP Accept header is disabled by default now. You should prefer the use of formatted URLs (such as <code>/customers/1.xml</code>) to indicate the format that you want. If you need the Accept headers, you can turn them back on with <code>config.action_controller.use_accept_header = true</code>.</li>
<li>Benchmarking numbers are now reported in milliseconds rather than tiny fractions of seconds</li>
<li>Rails now supports HTTP-only cookies (and uses them for sessions), which help mitigate some cross-site scripting risks in newer browsers.</li>
<li>
<code>redirect_to</code> now fully supports URI schemes (so, for example, you can redirect to a svn`ssh: URI).</li>
<li>
<code>render</code> now supports a <code>:js</code> option to render plain vanilla JavaScript with the right mime type.</li>
<li>Request forgery protection has been tightened up to apply to HTML-formatted content requests only.</li>
<li>Polymorphic URLs behave more sensibly if a passed parameter is nil. For example, calling <code>polymorphic_path([@project, @date, @area])</code> with a nil date will give you <code>project_area_path</code>.</li>
</ul>
<h3 id="action-view">7 Action View</h3>
<ul>
<li>
<code>javascript_include_tag</code> and <code>stylesheet_link_tag</code> support a new <code>:recursive</code> option to be used along with <code>:all</code>, so that you can load an entire tree of files with a single line of code.</li>
<li>The included Prototype JavaScript library has been upgraded to version 1.6.0.3.</li>
<li>
<code>RJS#page.reload</code> to reload the browser's current location via JavaScript</li>
<li>The <code>atom_feed</code> helper now takes an <code>:instruct</code> option to let you insert XML processing instructions.</li>
</ul>
<h3 id="action-mailer">8 Action Mailer</h3><p>Action Mailer now supports mailer layouts. You can make your HTML emails as pretty as your in-browser views by supplying an appropriately-named layout - for example, the <code>CustomerMailer</code> class expects to use <code>layouts/customer_mailer.html.erb</code>.</p>
<ul>
<li>More information:

<ul>
<li><a href="http://archives.ryandaigle.com/articles/2008/9/7/what-s-new-in-edge-rails-mailer-layouts">What's New in Edge Rails: Mailer Layouts</a></li>
</ul>
</li>
</ul>
<p>Action Mailer now offers built-in support for GMail's SMTP servers, by turning on STARTTLS automatically. This requires Ruby 1.8.7 to be installed.</p><h3 id="active-support">9 Active Support</h3><p>Active Support now offers built-in memoization for Rails applications, the <code>each_with_object</code> method, prefix support on delegates, and various other new utility methods.</p><h4 id="memoization">9.1 Memoization</h4><p>Memoization is a pattern of initializing a method once and then stashing its value away for repeat use. You've probably used this pattern in your own applications:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
def full_name
  @full_name ||= "#{first_name} #{last_name}"
end

</pre>
</div>
<p>Memoization lets you handle this task in a declarative fashion:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
extend ActiveSupport::Memoizable

def full_name
  "#{first_name} #{last_name}"
end
memoize :full_name

</pre>
</div>
<p>Other features of memoization include <code>unmemoize</code>, <code>unmemoize_all</code>, and <code>memoize_all</code> to turn memoization on or off.</p>
<ul>
<li>Lead Contributor: <a href="http://joshpeek.com/">Josh Peek</a>
</li>
<li>More information:

<ul>
<li><a href="http://archives.ryandaigle.com/articles/2008/7/16/what-s-new-in-edge-rails-memoization">What's New in Edge Rails: Easy Memoization</a></li>
<li><a href="http://www.railway.at/articles/2008/09/20/a-guide-to-memoization">Memo-what? A Guide to Memoization</a></li>
</ul>
</li>
</ul>
<h4 id="each-with-object">9.2 each_with_object</h4><p>The <code>each_with_object</code> method provides an alternative to <code>inject</code>, using a method backported from Ruby 1.9. It iterates over a collection, passing the current element and the memo into the block.</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
%w(foo bar).each_with_object({}) { |str, hsh| hsh[str] = str.upcase } # =&gt; {'foo' =&gt; 'FOO', 'bar' =&gt; 'BAR'}

</pre>
</div>
<p>Lead Contributor: <a href="http://therealadam.com/">Adam Keys</a></p><h4 id="delegates-with-prefixes">9.3 Delegates With Prefixes</h4><p>If you delegate behavior from one class to another, you can now specify a prefix that will be used to identify the delegated methods. For example:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class Vendor &lt; ActiveRecord::Base
  has_one :account
  delegate :email, :password, :to =&gt; :account, :prefix =&gt; true
end

</pre>
</div>
<p>This will produce delegated methods <code>vendor#account_email</code> and <code>vendor#account_password</code>. You can also specify a custom prefix:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class Vendor &lt; ActiveRecord::Base
  has_one :account
  delegate :email, :password, :to =&gt; :account, :prefix =&gt; :owner
end

</pre>
</div>
<p>This will produce delegated methods <code>vendor#owner_email</code> and <code>vendor#owner_password</code>.</p><p>Lead Contributor: <a href="http://workingwithrails.com/person/5830-daniel-schierbeck">Daniel Schierbeck</a></p><h4 id="other-active-support-changes">9.4 Other Active Support Changes</h4>
<ul>
<li>Extensive updates to <code>ActiveSupport::Multibyte</code>, including Ruby 1.9 compatibility fixes.</li>
<li>The addition of <code>ActiveSupport::Rescuable</code> allows any class to mix in the <code>rescue_from</code> syntax.</li>
<li>
<code>past?</code>, <code>today?</code> and <code>future?</code> for <code>Date</code> and <code>Time</code> classes to facilitate date/time comparisons.</li>
<li>
<code>Array#second</code> through <code>Array#fifth</code> as aliases for <code>Array#[1]</code> through <code>Array#[4]</code>
</li>
<li>
<code>Enumerable#many?</code> to encapsulate <code>collection.size &gt; 1</code>
</li>
<li>
<code>Inflector#parameterize</code> produces a URL-ready version of its input, for use in <code>to_param</code>.</li>
<li>
<code>Time#advance</code> recognizes fractional days and weeks, so you can do <code>1.7.weeks.ago</code>, <code>1.5.hours.since</code>, and so on.</li>
<li>The included TzInfo library has been upgraded to version 0.3.12.</li>
<li>
<code>ActiveSupport::StringInquirer</code> gives you a pretty way to test for equality in strings: <code>ActiveSupport::StringInquirer.new("abc").abc? =&gt; true</code>
</li>
</ul>
<h3 id="railties">10 Railties</h3><p>In Railties (the core code of Rails itself) the biggest changes are in the <code>config.gems</code> mechanism.</p><h4 id="config-gems">10.1 config.gems</h4><p>To avoid deployment issues and make Rails applications more self-contained, it's possible to place copies of all of the gems that your Rails application requires in <code>/vendor/gems</code>. This capability first appeared in Rails 2.1, but it's much more flexible and robust in Rails 2.2, handling complicated dependencies between gems. Gem management in Rails includes these commands:</p>
<ul>
<li>
<code>config.gem _gem_name_</code> in your <code>config/environment.rb</code> file</li>
<li>
<code>rake gems</code> to list all configured gems, as well as whether they (and their dependencies) are installed, frozen, or framework (framework gems are those loaded by Rails before the gem dependency code is executed; such gems cannot be frozen)</li>
<li>
<code>rake gems:install</code> to install missing gems to the computer</li>
<li>
<code>rake gems:unpack</code> to place a copy of the required gems into <code>/vendor/gems</code>
</li>
<li>
<code>rake gems:unpack:dependencies</code> to get copies of the required gems and their dependencies into <code>/vendor/gems</code>
</li>
<li>
<code>rake gems:build</code> to build any missing native extensions</li>
<li>
<code>rake gems:refresh_specs</code> to bring vendored gems created with Rails 2.1 into alignment with the Rails 2.2 way of storing them</li>
</ul>
<p>You can unpack or install a single gem by specifying <code>GEM=_gem_name_</code> on the command line.</p>
<ul>
<li>Lead Contributor: <a href="http://github.com/al2o3cr">Matt Jones</a>
</li>
<li>More information:

<ul>
<li><a href="http://archives.ryandaigle.com/articles/2008/4/1/what-s-new-in-edge-rails-gem-dependencies">What's New in Edge Rails: Gem Dependencies</a></li>
<li><a href="http://afreshcup.com/2008/10/25/rails-212-and-22rc1-update-your-rubygems/">Rails 2.1.2 and 2.2RC1: Update Your RubyGems</a></li>
<li><a href="http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/1128">Detailed discussion on Lighthouse</a></li>
</ul>
</li>
</ul>
<h4 id="other-railties-changes">10.2 Other Railties Changes</h4>
<ul>
<li>If you're a fan of the <a href="http://code.macournoyer.com/thin/">Thin</a> web server, you'll be happy to know that <code>script/server</code> now supports Thin directly.</li>
<li>
<code>script/plugin install &amp;lt;plugin&amp;gt; -r &amp;lt;revision&amp;gt;</code> now works with git-based as well as svn-based plugins.</li>
<li>
<code>script/console</code> now supports a <code>--debugger</code> option</li>
<li>Instructions for setting up a continuous integration server to build Rails itself are included in the Rails source</li>
<li>
<code>rake notes:custom ANNOTATION=MYFLAG</code> lets you list out custom annotations.</li>
<li>Wrapped <code>Rails.env</code> in <code>StringInquirer</code> so you can do <code>Rails.env.development?</code>
</li>
<li>To eliminate deprecation warnings and properly handle gem dependencies, Rails now requires rubygems 1.3.1 or higher.</li>
</ul>
<h3 id="deprecated">11 Deprecated</h3><p>A few pieces of older code are deprecated in this release:</p>
<ul>
<li>
<code>Rails::SecretKeyGenerator</code> has been replaced by <code>ActiveSupport::SecureRandom</code>
</li>
<li>
<code>render_component</code> is deprecated. There's a <a href="http://github.com/rails/render_component/tree/master">render_components plugin</a> available if you need this functionality.</li>
<li>
<p>Implicit local assignments when rendering partials has been deprecated.</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
def partial_with_implicit_local_assignment
  @customer = Customer.new("Marcel")
  render :partial =&gt; "customer"
end

</pre>
</div>
<p>Previously the above code made available a local variable called <code>customer</code> inside the partial 'customer'. You should explicitly pass all the variables via :locals hash now.</p>
</li>
<li><p><code>country_select</code> has been removed. See the <a href="http://www.rubyonrails.org/deprecation/list-of-countries">deprecation page</a> for more information and a plugin replacement.</p></li>
<li><p><code>ActiveRecord::Base.allow_concurrency</code> no longer has any effect.</p></li>
<li><p><code>ActiveRecord::Errors.default_error_messages</code> has been deprecated in favor of <code>I18n.translate('activerecord.errors.messages')</code></p></li>
<li><p>The <code>%s</code> and <code>%d</code> interpolation syntax for internationalization is deprecated.</p></li>
<li><p><code>String#chars</code> has been deprecated in favor of <code>String#mb_chars</code>.</p></li>
<li><p>Durations of fractional months or fractional years are deprecated. Use Ruby's core <code>Date</code> and <code>Time</code> class arithmetic instead.</p></li>
<li><p><code>Request#relative_url_root</code> is deprecated. Use <code>ActionController::Base.relative_url_root</code> instead.</p></li>
</ul>
<h3 id="credits">12 Credits</h3><p>Release notes compiled by <a href="http://afreshcup.com">Mike Gunderloy</a></p>

        <h3>反馈</h3>
        <p>
          我们鼓励您帮助提高本指南的质量。
        </p>
        <p>
          如果看到如何错字或错误，请反馈给我们。
          您可以阅读我们的<a href="http://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#contributing-to-the-rails-documentation">文档贡献</a>指南。
        </p>
        <p>
          您还可能会发现内容不完整或不是最新版本。
          请添加缺失文档到 master 分支。请先确认 <a href="http://edgeguides.rubyonrails.org">Edge Guides</a> 是否已经修复。
          关于用语约定，请查看<a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails 指南指导</a>。
        </p>
        <p>
          无论什么原因，如果你发现了问题但无法修补它，请<a href="https://github.com/rails/rails/issues">创建 issue</a>。
        </p>
        <p>
          最后，欢迎到 <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs 邮件列表</a>参与任何有关 Ruby on Rails 文档的讨论。
        </p>
        <h4>中文翻译反馈</h4>
        <p>贡献：<a href="https://github.com/ruby-china/guides">https://github.com/ruby-china/guides</a>。</p>
      </div>
    </div>
  </div>

  <hr class="hide" />
  <div id="footer">
    <div class="wrapper">
      <p>本著作采用 <a href="https://creativecommons.org/licenses/by-sa/4.0/">创作共用 署名-相同方式共享 4.0 国际</a> 授权</p>
<p>“Rails”，“Ruby on Rails”，以及 Rails Logo 为 David Heinemeier Hansson 的商标。版权所有</p>

    </div>
  </div>

  <script type="text/javascript" src="javascripts/jquery.min.js"></script>
  <script type="text/javascript" src="javascripts/responsive-tables.js"></script>
  <script type="text/javascript" src="javascripts/guides.js"></script>
  <script type="text/javascript" src="javascripts/syntaxhighlighter.js"></script>
  <script type="text/javascript">
    syntaxhighlighterConfig = {
      autoLinks: false,
    };
    $(guidesIndex.bind);
  </script>
</body>
</html>
